package com.example.arithmeticleetcode.leetcode;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-06-30 17:29
 **/
public class Demo36 {

    public static void main(String[] args) {
        int[] salary1 = {6000, 5000, 4000, 3000, 2000, 1000};
        System.out.println(average(salary1)); //3500.00000
        int[] salary2 = {1000, 2000, 3000};
        System.out.println(average(salary2)); //2000.00000
        int[] salary3 = {4000, 3000, 1000, 2000};
        System.out.println(average(salary3)); //2500.00000
        int[] salary4 = {8000, 9000, 2000, 3000, 6000, 1000};
        System.out.println(average(salary4)); //4750.00000
        int[] salary5 = {48000, 59000, 99000, 13000, 78000, 45000, 31000, 17000, 39000, 37000, 93000, 77000, 33000, 28000, 4000, 54000, 67000, 6000, 1000, 11000};
        System.out.println(average(salary5)); //41111.11111
    }

    public static double average(int[] salary) {
        Integer max = Integer.MIN_VALUE;
        Integer min = Integer.MAX_VALUE;
        Integer sum = 0;
        for (int i : salary) {
            if (min > i) {
                min = i;
            }
            if (max < i) {
                max = i;
            }
            sum += i;
        }
        return (double) (sum - min - max) / (double) (salary.length - 2);
    }
}
